package view;

import control.CustomerManagerControl;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class CustomerCategoryPanel extends RootPanel
{

    private int choice = 0;
    private int index;
    private CustomerManagerControl cmControl = new CustomerManagerControl();

    public CustomerCategoryPanel(JFrame window)
    {
        initComponents();
        this.window = window;
        jTDanhmucKH.addMouseListener(new MouseAdapter()
        {
            @Override
            public void mousePressed(MouseEvent me)
            {
                index = jTDanhmucKH.getSelectedRow();
                choice = (int) jTDanhmucKH.getValueAt(index, 0);
                jTFKhachhangduocchon.setText(jTDanhmucKH.getValueAt(index, 1).toString());
            }
        });
        jCBTen.setSelected(true);
        jCBDienthoai.setSelected(false);
        jCBCMTND.setSelected(false);
        setBgSearch(50, 95);
        setBgSearch(50, 485);
        setTime();
        setBackground("customer_panel");
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents()
    {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTDanhmucKH = new javax.swing.JTable();
        jBSuaKH = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jBThemKH = new javax.swing.JButton();
        jBXoaKH = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jTFTimkiem = new javax.swing.JTextField();
        jButton4 = new javax.swing.JButton();
        jCBTen = new javax.swing.JCheckBox();
        jCBCMTND = new javax.swing.JCheckBox();
        jCBDienthoai = new javax.swing.JCheckBox();
        jTFKhachhangduocchon = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();

        jTDanhmucKH.setModel(cmControl.getCustomerCategory());
        jTDanhmucKH.setAutoscrolls(false);
        jScrollPane1.setViewportView(jTDanhmucKH);

        jBSuaKH.setText("Chỉnh sửa khách hàng");
        jBSuaKH.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                jBSuaKHActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(0, 51, 153));
        jLabel1.setText("DANH MỤC KHÁCH HÀNG");
        jLabel1.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);

        jBThemKH.setText("Thêm khách hàng");
        jBThemKH.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                jBThemKHActionPerformed(evt);
            }
        });

        jBXoaKH.setText("Xóa khách hàng");
        jBXoaKH.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                jBXoaKHActionPerformed(evt);
            }
        });

        jLabel2.setText("Tìm kiếm theo: ");

        jButton4.setText(" Tìm");
        jButton4.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                jButton4ActionPerformed(evt);
            }
        });

        jCBTen.setText("Tên");
        jCBTen.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                jCBTenActionPerformed(evt);
            }
        });

        jCBCMTND.setText("CMTND");
        jCBCMTND.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                jCBCMTNDActionPerformed(evt);
            }
        });

        jCBDienthoai.setText("Điện thoại");
        jCBDienthoai.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                jCBDienthoaiActionPerformed(evt);
            }
        });

        jTFKhachhangduocchon.setEditable(false);
        jTFKhachhangduocchon.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel3.setText("Khách hàng được chọn");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(329, 329, 329)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 216, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(52, 52, 52)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 564, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(jBSuaKH, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 140, Short.MAX_VALUE)
                                    .addComponent(jBThemKH, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jBXoaKH, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(18, 18, 18)
                                .addComponent(jLabel2)
                                .addGap(18, 18, 18)
                                .addComponent(jCBTen)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jCBCMTND)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jCBDienthoai)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jTFTimkiem, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jButton4))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(30, 30, 30)
                                .addComponent(jLabel3)
                                .addGap(18, 18, 18)
                                .addComponent(jTFKhachhangduocchon, javax.swing.GroupLayout.PREFERRED_SIZE, 178, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap(62, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addComponent(jLabel1)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(148, 148, 148)
                        .addComponent(jBThemKH, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jBSuaKH, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jBXoaKH, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(54, 54, 54)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(jCBTen)
                            .addComponent(jCBCMTND)
                            .addComponent(jCBDienthoai)
                            .addComponent(jTFTimkiem, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jButton4))
                        .addGap(34, 34, 34)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 316, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTFKhachhangduocchon, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addContainerGap(183, Short.MAX_VALUE))
        );

        jLabel1.getAccessibleContext().setAccessibleDescription("");
    }// </editor-fold>//GEN-END:initComponents

    private void jBSuaKHActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBSuaKHActionPerformed
        if (choice == 0)
        {
            JOptionPane.showMessageDialog(null, "Bạn chưa chọn khách hàng");
        }
        else
        {
            EditCustomerDialog edDialog = new EditCustomerDialog(choice, this, window);
            edDialog.setVisible(true);
            window.setEnabled(false);
            edDialog.addWindowListener(onClose);
            reset();
        }
    }//GEN-LAST:event_jBSuaKHActionPerformed

    private void jBThemKHActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBThemKHActionPerformed
        AddCustomerDialog adDialog = new AddCustomerDialog(this);
        adDialog.setVisible(true);
        window.setEnabled(false);
        adDialog.addWindowListener(onClose);
        reset();
    }//GEN-LAST:event_jBThemKHActionPerformed

    private void jBXoaKHActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBXoaKHActionPerformed
        if (choice == 0)
        {
            JOptionPane.showMessageDialog(null, "Chưa chọn khách hàng");
        }
        else
        {
            try
            {
                if (cmControl.TestInforCustomer(choice) == false)
                {
                    JOptionPane.showMessageDialog(null, "Không thể xóa");
                }
                else
                {
                    if (JOptionPane.showConfirmDialog(this, "Bạn chắc chắn muốn xóa?",
                            "Xóa khách hàng", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
                    {
                        cmControl.deleteCustomer(choice);
                        JOptionPane.showMessageDialog(null, "Xóa thành công");
                    }
                }
            }
            catch (SQLException ex)
            {
                Logger.getLogger(CustomerCategoryPanel.class.getName()).log(Level.SEVERE, null, ex);
            }
            choice = 0;
            reset();
        }
    }//GEN-LAST:event_jBXoaKHActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        if (jCBTen.getSelectedObjects() != null)
        {
            String key = jTFTimkiem.getText();
            jTDanhmucKH.setModel(cmControl.filterNameCustomer(key));
        }
        else
        {
            if (jCBDienthoai.getSelectedObjects() != null)
            {
                String key = jTFTimkiem.getText();
                jTDanhmucKH.setModel(cmControl.filterPhoneCustomer(key));
            }
            else
            {
                String key = jTFTimkiem.getText();
                jTDanhmucKH.setModel(cmControl.filterCMTNDCustomer(key));
            }
        }


    }//GEN-LAST:event_jButton4ActionPerformed

    private void jCBTenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCBTenActionPerformed
        jTFTimkiem.setText(null);
        jCBDienthoai.setSelected(false);
        jCBCMTND.setSelected(false);
    }//GEN-LAST:event_jCBTenActionPerformed

    private void jCBCMTNDActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCBCMTNDActionPerformed
        jTFTimkiem.setText(null);
        jCBTen.setSelected(false);
        jCBDienthoai.setSelected(false);
    }//GEN-LAST:event_jCBCMTNDActionPerformed

    private void jCBDienthoaiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCBDienthoaiActionPerformed
        jTFTimkiem.setText(null);
        jCBTen.setSelected(false);
        jCBCMTND.setSelected(false);
    }//GEN-LAST:event_jCBDienthoaiActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBSuaKH;
    private javax.swing.JButton jBThemKH;
    private javax.swing.JButton jBXoaKH;
    private javax.swing.JButton jButton4;
    private javax.swing.JCheckBox jCBCMTND;
    private javax.swing.JCheckBox jCBDienthoai;
    private javax.swing.JCheckBox jCBTen;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTDanhmucKH;
    private javax.swing.JTextField jTFKhachhangduocchon;
    private javax.swing.JTextField jTFTimkiem;
    // End of variables declaration//GEN-END:variables

    @Override
    public void Show()
    {
        super.Show();
        reset();
    }

    public void reset()
    {
        jTDanhmucKH.setModel(cmControl.getCustomerCategory());
        jTDanhmucKH.getTableHeader().setReorderingAllowed(false);
        jCBCMTND.setSelected(false);
        jCBDienthoai.setSelected(false);
        jCBTen.setSelected(true);
        jTFKhachhangduocchon.setText(null);
        jTFTimkiem.setText(null);
        choice = 0;
    }
}
